
import { _decorator, Component, Node, Collider, ITriggerEvent, AudioSource } from 'cc';
import { Constant } from '../framework/Constant';
const { ccclass, property } = _decorator;

@ccclass('SelfPlane')
export class SelfPlane extends Component {
    @property(Node)
    public explode:Node=null;
    @property(Node)
    public booldFace:Node=null;
    @property(Node)
    public boold:Node=null;
    
    public lifeValue=5;
    public _isDie=false;

    private _currLife=0;
    private _audioSource :AudioSource=null;
   
    start()
    {
        this._audioSource=this.getComponent(AudioSource);
    }
    onEnable () {
        const  collider=this.getComponent(Collider);
        collider.on('onTriggerEnter',this._onTriggerEnter,this);
    }

    onDisable () {
        const  collider=this.getComponent(Collider);
        collider.off('onTriggerEnter',this._onTriggerEnter,this);
    }

    public _init()
    {
        this._currLife=this.lifeValue;
        this._isDie=false;
        this.explode.active=false;
        this.booldFace.setScale(1,1,1);
    }
    // update (deltaTime: number) {
    //     // [4]
    // }g
    private _onTriggerEnter(event:ITriggerEvent)
    {
       const collisionGroup=event.otherCollider.getGroup();
       if(collisionGroup===Constant.CollisionType.ENEMY_BULLET||collisionGroup===Constant.CollisionType.ENEMY_BULLET)
       {
           if(this._currLife==this.lifeValue)
           {
               this.boold.active=true;
           }
             this._currLife--;
             this.booldFace.setScale(this._currLife/this.lifeValue,1,1);
             if(this._currLife<=0)
             {
                 this._isDie=true;
                 this._audioSource.play();
                 this.explode.active=true;
             }
       }
    }
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.0/manual/en/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.0/manual/en/scripting/ccclass.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.0/manual/en/scripting/life-cycle-callbacks.html
 */
